<script type="text/javascript">
$(document).ready(function(){
	
	$("#frmAEMenu").submit(function(){
		var msg = "";
		if($("#name").attr("value") == ""){
			msg +="Thiếu tên menu.\n";
		}
		if($("#link").attr("value") == ""){
			msg +="Chưa chọn link.";
		}
		if(msg != ""){
			alert(msg);
			return false;
		}
	});
	$("#slcPage").change(function(){
		if($(this).attr("value") == 0){
			$("#link").attr("value","");
		}else{
			$.ajax({
				url: "<?php echo $this->url(array(), "admin_ajax_pageslug"); ?>",
				data: "page_id="+ $(this).attr("value"),
				type: "GET",
				dataType: "html",
				success: function(data){
					$("#link").attr("value",data);
				}
			});
		}
	});
	//xóa
	$("#btnDelete").click(function(){
		var valid= false;
		var arrChkId = $(".chkId");
		var strIDs = "";
		for(var i =0; i < arrChkId.length; i++){
			if(arrChkId.eq(i).attr("checked")){
				valid = true;
				if(strIDs!=""){
					strIDs +=","+ arrChkId.eq(i).attr("value");
				}else{
					strIDs +=arrChkId.eq(i).attr("value");
				}
				//break;
			}
		}
		if(valid){
			if(confirm("Bạn thực sự muốn xóa những menu này?")){
				$("#hddIDs").attr("value", strIDs);
				$("#frmActions").attr("action", "<?php echo $this->url(array(), 'admin_menu_delete');?>");
			}else{
				return false;
			}
		}else{
			alert("Xin hãy chọn menu muốn xóa!");
			return false;
		}
	});
	
});
</script>
<div id="r-heading">
	<img src="<?php echo PUBLIC_URL.'/admin/images/menu.png';?>" alt=""/>
	<span>Danh sách menu</span>
</div>
<?php
//nếu có message thì hiển thị
if(!empty($this->message)){
	echo '<div id="message">'.$this->message.'</div>';
} 
?>
<div style="width: 40%; float: left;">
	<form id="frmAEMenu" action ="" method="post">
		<div class="row">
			<label for="name">Tên menu</label>
			<?php if(isset($this->menu)){?>
			<input type="text" name="name" id="name" size="50" value="<?php echo $this->menu->name;?>"/>
			<?php }else{?>
			<input type="text" name="name" id="name" size="50"/>
			<?php }?>
		</div>
		<div class="row">
			<label for="name">Link tới</label>
			<select id="slcPage">
				<option value="0">Chọn trang</option>
				<?php
				foreach ($this->arrPages as $page){
					echo "<option value='".$page->id."'>".$page->title."</option>";
				} 
				?>
			</select>
		</div>
		<div class="row">
			<label for="link">Link tới</label>
			<?php if(isset($this->menu)){?>
			<input type="text" name="link" id="link" size="50" value="<?php echo $this->menu->link;?>"/>
			<?php }else{?>
			<input type="text" name="link" id="link" size="50"/>
			<?php }?>
		</div>
		<div class="row">
			<label for="priority">Sắp xếp (nhỏ hiển thị trước)</label>
			<?php if(isset($this->menu)){?>
			<input type="text" name="priority" id="priority" size="10" value="<?php echo $this->menu->priority;?>"/>
			<?php }else{?>
			<input type="text" name="priority" id="priority" size="10"/>
			<?php }?>
		</div>
		<div class="row">
			<input type="submit" value="Lưu" class="button"/>
			<input type="reset" value="Làm lại" class="button"/>
		</div>
	</form>
</div>

<div style="width: 60%; float: left;">
	<form id="frmActions" action="" method="post">
		<div id="actions">
			<!-- <input type="text" id="txtKeyword" name="keyword" class="text" value="" size="25"/>
			<input type="submit" id="btnSearch" class="button" value="Tìm kiếm"/>-->
			<input type="submit" id="btnDelete" class="button" value="Xóa các mục đã chọn"/>
			<input type="hidden" id="hddIDs" value="" name="ids"/>
		</div>
		<table class="list" cellspacing="0">
			<thead>
				<tr>
					<td width="5%">
						<input type="checkbox" class="chkAll"/>
					</td>
					<td width="20%">
						Tên menu
					</td>
					<td width="45%">
						Link
					</td>
					<td width="10%">Sắp xếp</td>
					<td width="15%">
						Thực hiện
					</td>
				</tr>
			</thead>
			<tbody>
				<?php
					$count = count($this->arrMenus);
					if($count == 0){
				?>
					<tr>
						<td colspan="4" style="color: #fc0202;">
							Chưa có menu nào trong cơ sở dữ liệu!
						</td>
					</tr>
				<?php
					}else{
						for($i = 0; $i< $count; $i++){
				?>
					<tr <?php if($i == $count - 1) echo 'class="last"';?>>
						<td>
							<input type="checkbox" class="chkId" value="<?php echo $this->arrMenus[$i]->id; ?>"/>
						</td>
						<td>
							<a href="<?php echo  $this->url(array('id' => $this->arrMenus[$i]->id), 'admin_menu');?>" title="Sửa thông tin menu">
								<?php
									echo $this->arrMenus[$i]->name;
								?>
							</a>
						</td>
						<td>
							<a href="<?php echo $this->arrMenus[$i]->link;?>"><?php echo $this->arrMenus[$i]->link;?></a>
						</td>
						<td>
							<?php echo $this->arrMenus[$i]->priority?>
						</td>
						<td>
							<a href="<?= $this->url(array('id' => $this->arrMenus[$i]->id), 'admin_menu');?>" title="Sửa thông tin menu">Sửa</a>
							|
							<a href="<?= $this->url(array('id' => $this->arrMenus[$i]->id), 'admin_menu_delete');?>" onclick="if(!confirm('Bạn thực sự muốn xóa menu này?')) return false;">Xóa</a>
						</td>
					</tr>
				<?php
						}
					}
				?>
			</tbody>
			<tfoot>
				<tr>
					<td>
						<input type="checkbox" class="chkAll"/>
					</td>
					<td>
						Tên
					</td>
					<td>
						Số bài viết
					</td>
					<td>
						Sắp xếp
					</td>
					<td>
						Thực hiện
					</td>
				</tr>
			</tfoot>
		</table>
	</form>
</div>
